import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'
import goodsRouter from '@/router/modules/goods'
import membersRouter from '@/router/modules/members'
Vue.use(Router)

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/purchasing',
    name: 'Purchasing',
    component: Layout,
    redirect: 'purchasing-list',
    children: [
      {
        path: 'purchasing-list',
        name: 'PurchasingList',
        component: () => import('@/views/Purchasing'),
        meta: { title: '采购中心', icon: 'icon-comment' }
      },
      {
        path: 'purchasing-details',
        name: 'purchasingDetails',
        component: () => import('@/views/Purchasing/components/Details'),
        meta: { title: '采购详情' },
        hidden: true
      }
    ]
  },
  {
    path: '/storemanagement',
    name: 'StoreManagement',
    component: Layout,
    redirect: 'storemanagement-list',
    meta: { title: '门店管理', icon: 'icon-management' },
    children: [
      {
        path: 'storemanagement-list',
        name: 'StoreManagementList',
        component: () => import('@/views/StoreManagement/store/index'),
        meta: { title: '门店信息' }
      },
      {
        hidden: true,
        path: 'storemanagement-add',
        name: 'StoreManagementAdd',
        component: () => import('@/views/StoreManagement/store/salesroomAdd'),
        meta: { title: '新增门店' }
      },
      {
        hidden: true,
        path: 'storemanagement-detail',
        name: 'StoreManagementDetail',
        component: () => import('@/views/StoreManagement/store/components/detail'),
        meta: { title: '门店详情' }
      },
      {
        path: 'storemanagementinfo-list',
        name: 'storemanagementinforList',
        component: () => import('@/views/StoreManagement/details'),
        meta: { title: '门店销售单' }
      },
      {
        hidden: true,
        path: 'storemanagement-infor',
        name: 'StoreManagementInfor',
        component: () => import('@/views/StoreManagement/details/detail'),
        meta: { title: '销售到详情' }
      },
      {
        path: 'member-stores',
        name: 'MemberStores',
        component: () => import('@/views/StoreManagement/memberStores'),
        meta: { title: '门店会员信息' }
      },
      {
        hidden: true,
        path: 'member-detail/:id',
        name: 'MemberDetail',
        component: () => import('@/views/StoreManagement/memberStores/detail'),
        meta: { title: '门店会员详情' }
      }
    ]
  }
]

export const defaultRoute = [
  {
    path: '/',
    component: Layout,
    redirect: ''
    // children: [{
    //   path: 'dashboard',
    //   name: 'Dashboard',
    //   component: () => import('@/views/dashboard/index'),
    //   meta: { title: '首页', icon: 'dashboard' }
    // }]
  }
]

// 后面所有开发的功能模块路由，都添加在 [ asyncRoutes ] 里面
export const asyncRoutes = [
  goodsRouter,
  {
    path: '/ordersCentre',
    component: Layout,
    redirect: 'OrdersCentre-list',
    name: 'OrdersCentre',
    meta: { title: '订单中心', icon: 'table' },
    children: [
      {
        path: 'ordersCentre-list',
        name: 'OrdersCentre-list',
        component: () => import('@/views/OrdersCentre/index'),
        meta: { title: '订单中心', icon: 'icon-order_center' }
      },
      {
        path: 'ordersCentre-detail',
        name: 'ordersCentreDetail',
        component: () => import('@/views/OrdersCentre/detail/detail'),
        meta: { title: '订单详情' },
        hidden: true
      }
    ]
  },
  {
    path: '/supplier',
    name: 'Supplier',
    component: Layout,
    redirect: 'supplier-list',
    meta: { title: '供应商管理', icon: 'icon-supplier' },
    children: [
      {
        path: 'supplier-list',
        name: 'SupplierList',
        component: () => import('@/views/Supplier'),
        meta: { title: '供应商管理', icon: 'icon-supplier' }
      }
    ]
  },
  {
    path: '/afterSale',
    name: 'AfterSale',
    component: Layout,
    redirect: '/AfterSale/sales',
    meta: { title: '售后管理', icon: 'icon-after_sale' },
    children: [
      {
        path: 'sales',
        name: 'sales',
        component: () => import('@/views/AfterSale/index'),
        meta: { title: '售后管理', icon: 'icon-after_sale' }
      },
      {
        path: 'sales-detail',
        name: 'salesDetail',
        component: () => import('@/views/AfterSale/saleDetail/saleDetail'),
        meta: { title: '售后详情' },
        hidden: true
      }
    ]
  },
  {
    path: '/Comment',
    name: 'Comment',
    component: Layout,
    redirect: '/Comment/list',
    meta: { title: '评论管理', icon: 'icon-comment' },
    children: [
      {
        path: 'comment-list',
        name: 'comment-list',
        component: () => import('@/views/Comment/index'),
        meta: { title: '评论管理', icon: 'icon-comment' }
      }
    ]
  },
  membersRouter,
  {
    path: '/authority',
    name: 'Authority',
    component: Layout,
    redirect: 'role-list',
    meta: { title: '权限管理', icon: 'icon-management' },
    children: [
      {
        path: 'role-list',
        name: 'RoleList',
        component: () => import('@/views/Authority/role'),
        meta: { title: '角色管理' }
      },
      {
        hidden: true,
        path: 'role-add',
        name: 'roleAdd',
        component: () => import('@/views/Authority/role/roleDetail'),
        meta: { title: '新增角色' }
      },
      {
        hidden: true,
        path: 'role-edact/:id',
        name: 'rolerEdact',
        component: () => import('@/views/Authority/role/roleDetail'),
        meta: { title: '编辑角色' }
      },
      {
        path: 'user-list',
        name: 'UserList',
        component: () => import('@/views/Authority/user'),
        meta: { title: '用户管理' }
      }
    ]
  },
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

// router.beforeEach((to, from, next) => {
//   // 如果前往登陆页面放行
//   if (to.path === '/login') { return next() }
//   // 读取客户端sessionStorage 中存储的Token
//   const tokenStr = window.sessionStorage.getItem('Authorization', 'token')
//   // 判断Token是否存在，存在则放行，不存在则需进行登录
//   if (!tokenStr) {
//     return next('/login')
//   }
//   next()
// })
export default router
